package com.mo.axion.handler.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mo.axion.common.constant.CommonConstant;
import com.mo.axion.common.domain.AnchorInfo;
import com.mo.axion.common.domain.TaskInfo;
import com.mo.axion.common.enums.AnchorState;
import com.mo.axion.common.pipeline.BusinessProcess;
import com.mo.axion.common.pipeline.ProcessContext;
import com.mo.axion.support.service.ConfigService;
import com.mo.axion.support.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Mo
 * @createTime 2024/2/23 11:37
 * @description 丢弃信息 将需要丢弃的模板id写在分布式配置中心
 */
@Service
public class DiscardAction implements BusinessProcess<TaskInfo> {

    private static final String DISCARD_MESSAGE_KEY = "discardMsgIds";

    @Autowired
    private ConfigService config;
    @Autowired
    private LogUtils logUtils;

    /**
     * 真正处理逻辑
     *
     * @param context
     */
    @Override
    public void process(ProcessContext<TaskInfo> context) {
        TaskInfo taskInfo = context.getProcessModel();
        JSONArray array = JSON.parseArray(config.getProperty(DISCARD_MESSAGE_KEY, CommonConstant.EMPTY_VALUE_JSON_ARRAY));
        if (array.contains(String.valueOf(taskInfo.getMessageTemplateId()))) {
            logUtils.print(
                    AnchorInfo.builder()
                            .bizId(taskInfo.getBizId())
                            .messageId(taskInfo.getMessageId())
                            .businessId(taskInfo.getBusinessId())
                            .idList(taskInfo.getReceiver())
                            .state(AnchorState.DISCARD.getCode())
                            .build()
            );
            context.setIsNeedBreak(true);
        }
    }
}
